package io.renren.modules.workflow.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.workflow.dao.ActBusFormDefinitionMapper;
import io.renren.modules.workflow.entity.ActBusFormDefinition;
import io.renren.modules.workflow.service.IActBusFormDefinitionService;
import io.renren.modules.workflow.service.IActBusFormMetadataFieldService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Desc 流程自定义表单 服务实现类
 * @Author landlord
 * @Date 2021/7/13 16:55
 */
@Service
public class ActBusFormDefinitionServiceImpl implements IActBusFormDefinitionService {

    @Autowired
    private ActBusFormDefinitionMapper actBusFormDefinitionMapper;

    @Autowired
    private IActBusFormMetadataFieldService actBusFormMetadataFieldService;
    /**
     * 查询流程单自定义
     *
     * @param abfdId 流程单自定义ID
     * @return 流程单自定义
     */
    @Override
    public ActBusFormDefinition selectActBusFormDefinitionById(String abfdId) {
        return actBusFormDefinitionMapper.selectActBusFormDefinitionById(abfdId);
    }

    /**
     * 查询流程单自定义列表
     *
     * @param actBusFormDefinition 流程单自定义
     * @return 流程单自定义
     */
    @Override
    public List<ActBusFormDefinition> selectActBusFormDefinitionList(ActBusFormDefinition actBusFormDefinition) {
        return actBusFormDefinitionMapper.selectActBusFormDefinitionList(actBusFormDefinition);
    }

    /**
     * 新增流程单自定义
     *
     * @param actBusFormDefinition 流程单自定义
     * @return 结果
     */
    @Override
    public int insertActBusFormDefinition(ActBusFormDefinition actBusFormDefinition) {
        actBusFormDefinition.setAbfdId(UUID.randomUUID().toString());
        actBusFormDefinition.setCreateBy(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername() );
        actBusFormDefinition.setCreateTime(new Date());
        return actBusFormDefinitionMapper.insertActBusFormDefinition(actBusFormDefinition);
    }

    /**
     * 修改流程单自定义
     *
     * @param actBusFormDefinition 流程单自定义
     * @return 结果
     */
    @Override
    public int updateActBusFormDefinition(ActBusFormDefinition actBusFormDefinition) {
        actBusFormDefinition.setUpdateBy(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
        actBusFormDefinition.setUpdateTime(new Date());
        return actBusFormDefinitionMapper.updateActBusFormDefinition(actBusFormDefinition);
    }

    /**
     * 批量删除流程单自定义
     *
     * @param abfdIds 需要删除的流程单自定义ID
     * @return 结果
     */
    @Override
    public int deleteActBusFormDefinitionByIds(String[] abfdIds) {
        return actBusFormDefinitionMapper.deleteActBusFormDefinitionByIds(abfdIds);
    }

    /**
     * 删除流程单自定义信息
     *
     * @param abfdId 流程单自定义ID
     * @return 结果
     */
    @Override
    public int deleteActBusFormDefinitionById(String abfdId) {
        return actBusFormDefinitionMapper.deleteActBusFormDefinitionById(abfdId);
    }

    /**
     * @Desc: 校验自定义表单 表单名称是否重复
     * @auther: landlord
     * @date: 2021/7/17 10:36
     * @return:
     */
    @Override
    public String checkActBusFormDefinitionUnique(ActBusFormDefinition actBusFormDefinition) {
        String abfdId = StrUtil.isEmpty(actBusFormDefinition.getAbfdId()) ? "-1" : actBusFormDefinition.getAbfdId();
        ActBusFormDefinition info = actBusFormDefinitionMapper.checkActBusFormDefinitionAbfdTitleUnique(actBusFormDefinition.getAbfdTitle());
        if (ObjectUtil.isNotNull(info) && !info.getAbfdId().equals(abfdId)) {
            return "1";
        }
        return "0";
    }

}
